{extends '../layout.latte'}

{block content}

<h1>My Amazing Blog</h1>
<p>Welcome to my blog!</p>
{if $username && permission('post.create')}
<p><a class="button" href="{route('blog_create')}">Create a new post</a></p>
{/if}
{foreach $posts as $post}
	{first}
	<h2>Recent Posts</h2>
	{/first}
	<hr>
	<h3><a href="{route('blog_show', [ 'id' => $post->id ])}">{$post->title}</a></h3>
	<p><small>By: {$post->username} on {$post->created_at|date:'d.m.Y G:i a'}</small></p>
	<p>Comments: {count($post->comments)}
	<p>{$post->content|truncate:100}</p>
	{if permission('post.update')}
	<hr>
	<a class="pseudo button" href="{route('blog_update', [ 'id' => $post->id ])}">Update</a>
	{/if}
	{if permission('post.delete')}
	 - 
	 <a class="pseudo button" href="{route('blog_destroy', [ 'id' => $post->id ])}">Delete</a>
	{/if}
{/foreach}

<p>
	{if $username === ''}
	<a href="{route('login')}">Login</a>
	{else}
	<a href="{route('logout')}">Logout</a></p>
	{/if}
{/block}